package study07_idle.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kdyzm
 */
@Slf4j
public class IdleSocketServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(!(evt instanceof IdleStateEvent)){
            super.userEventTriggered(ctx, evt);
            return;
        }
        IdleStateEvent event = (IdleStateEvent) evt;
        //事件状态描述对象
        String eventDes = null;
        switch (event.state()){
            case READER_IDLE:
                eventDes = "读空闲超时";
                break;
            case WRITER_IDLE:
                eventDes = "写空闲超时";
                break;
            case ALL_IDLE:
                eventDes = "读写空闲超时";
        }
        //出现读写空闲超时，关闭channel，以释放资源
        log.info(ctx.channel().remoteAddress()+":"+eventDes);
        ctx.channel().close();
    }
}
